﻿using Hotel.Model;
using MD5Hash;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class MemberDAL
    {
        public MemberDAL(HotelContext hotelContext)
        {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }

        /// <summary>
        /// 会员添加
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public int MemberAdd(Member members)
        {
            try
            {
                members.Pwd = members.Pwd.GetMD5();
                HotelContext.members.Add(members);
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 手机号和身份证号不能重复
        /// </summary>
        /// <param name="Phone"></param>
        /// <param name="Card"></param>
        /// <returns></returns>
        public int SearchPhoneCard(string Phone, string Card)
        {
            int res = HotelContext.members.Where(x => x.Phone == Phone).Count();
            if (res == -1)
            {
                return -1;//手机号不能重复
            }
            int err = HotelContext.members.Where(x => x.Card == Card).Count();
            if (err == -2)
            {
                return -2;//身份证号不能重复
            }
            return 1;
        }


        /// <summary>
        /// 会员列表显示
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="levelId"></param>
        /// <returns></returns>
        public List<Member> GetMembers(int? id = null, string? name = "", int? levelId = null)
        {
            var list = HotelContext.members.Where(x => x.IsDeleted == false).ToList();
            if (id != null)
            {
                list = list.Where(x => x.Id == id).ToList();
            }
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.Name.Contains(name)).ToList();
            }
            if (levelId != null)
            {
                list = list.Where(x => x.LevelId == levelId).ToList();
            }
            return list;
        }




        /// <summary>
        /// 会员等级添加
        /// </summary>
        /// <param name="memberLevel"></param>
        /// <returns></returns>
        public int MemberLevelAdd(MemberLevel memberLevel)
        {
            try
            {
                HotelContext.memberLevels.Add(memberLevel);
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员等级名称不能重复
        /// </summary>
        /// <param name="levelName"></param>
        /// <returns></returns>
        public int SearchLevelName(string levelName)
        {
            try
            {
                int res = HotelContext.memberLevels.Where(x => x.LevelName == levelName).Count();
                if (res==-1)
                {
                    return -1;//会员等级名称不能重复
                }
                return 1;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员等级列表显示
        /// </summary>
        /// <returns></returns>
        public List<MemberLevel> GetMemberLevels()
        {
            try
            {
                return HotelContext.memberLevels.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 根据id删除会员信息
        /// </summary>
        /// <param name="id">会员id</param>
        /// <returns>返回影响的行数</returns>
        public int DelMemberItem(int id)
        {
            try
            {
                //根据id找到实体
                var info = HotelContext.members.Find(id);
                //2.修改状态
                info.IsDeleted = true;
                //3.更新数据库
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 根据会员id获取会员实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Member GetMemberInfo(int id)
        {
            try
            {
                //根据id获取会员实体
                var info = HotelContext.members.Find(id);
                return info;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 修改会员
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public int UpdateMemberInfo(Member member)
        {
            try
            {
                //1.根据id获取旧实体
                var oldInfo = HotelContext.members.Find(member.Id);
                //2.修改
                oldInfo.Name = member.Name;
                oldInfo.Money = member.Money;
                oldInfo.Phone = member.Phone;
                oldInfo.Points = member.Points;
                oldInfo.Pwd = member.Pwd;
                oldInfo.Sex = member.Sex;
                oldInfo.UpdateTime = DateTime.Now;
                //3.更新数据库
                return HotelContext.SaveChanges() ;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员权益增加
        /// </summary>
        /// <param name="memberRight">权益实体</param>
        /// <returns></returns>
        public int MemberRightAdd(MemberRight memberRight)
        {
            try
            {
                HotelContext.memberRights.Add(memberRight);
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员权益名称是否重复
        /// </summary>
        /// <param name="rightName"></param>
        /// <returns></returns>
        public int MemberRightName(string rightName)
        {
            try
            {
                int res = HotelContext.memberRights.Where(x => x.RightName == rightName).Count();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 获取权益列表
        /// </summary>
        /// <param name="totalCount">总条数</param>
        /// <param name="totalPage">总页数</param>
        /// <param name="rightName">权益名称查询</param>
        /// <param name="createTime">创建时间查询</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        public List<MemberRight> MemberRightsList(out int totalCount,out int totalPage,string? rightName="",DateTime? date1 = null,DateTime ? date2 = null, int pageIndex=1,int pageSize=2)
        {
            try
            {
                //1.查询所有显示的数据
                List<MemberRight> list = HotelContext.memberRights.Where(x => x.IsDeleted == false).ToList();

                //2.根据权益名称查询
                if (!string.IsNullOrEmpty(rightName))
                {
                    list = list.Where(x=>x.RightName.Contains(rightName)).ToList();
                }
                //3.根据创建时间查询(根据生日查询一样)
                if (date1!=null && date2!=null)
                {
                    list = list.Where(x => x.CreateTime >= date1 && x.UpdateTime <= date2.Value.AddDays(1)).ToList();
                }

                //分页
                //计算总条数
                totalCount = list.Count();
                //计算总页数 总共13条 每页显示4条  应该分
                totalPage = (int)Math.Ceiling(totalCount*1.0 / pageSize);

                //分页
                list = list.OrderByDescending(x=>x.Id).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员权益逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelMemberRight(int id)
        {
            try
            {
                var res = HotelContext.memberRights.Find(id);
                res.IsDeleted = true;
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员权益反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MemberRight GetMemberRight(int id)
        {
            try
            {
                var res = HotelContext.memberRights.Find(id);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员权益修改
        /// </summary>
        /// <param name="memberRight"></param>
        /// <returns></returns>
        public int GetMemberRightUpd(MemberRight memberRight)
        {
            try
            {
                var info = HotelContext.memberRights.Find(memberRight.Id);
                info.RightName = memberRight.RightName;
                info.State = memberRight.State;
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据会员权益id修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int UpdateMemberRightState(int id,int state)
        {
            try
            {
                //1.根据id找到对应的实体
                var info = HotelContext.memberRights.Find(id);
                info.State = state;
                info.UpdateTime = DateTime.Now;
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
